package algocasts;

public class P1_isPalindrome {
    private boolean isAlphaOrNum(char c) {
        return (c >= '0' && c <= '9')
                || (c >= 'a' && c <= 'z')
                || (c >= 'A' && c <= 'Z');
    }

    private boolean isEqualIgnoreCase(char a, char b) {
        if (a >= 'A' && a <= 'Z') a += 32;
        if (b >= 'A' && b <= 'Z') b += 32;
        return a == b;
    }

    public boolean isPalindrome(String s) {
        if (s == null || s.length() == 0) return true;
        for (int left = 0, right = s.length() - 1; left < right; left++, right--) {
            while (left < right && !isAlphaOrNum(s.charAt(left))) left++;
            while (left < right && !isAlphaOrNum(s.charAt(right))) right--;
            if (left < right && !isEqualIgnoreCase(s.charAt(left), s.charAt(right)))
                return false;
        }
        return true;
    }

    public static void main(String[] args) {
        P1_isPalindrome solution = new P1_isPalindrome();
        System.out.println(solution.isPalindrome("\"A man, a plan, a canal: Panama\""));
    }

}
